package com.core.life.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.core.common.controller.BaseController;
import com.core.common.dao.Pager;
import com.core.common.pageModel.Combo;
import com.core.common.pageModel.EasyuiDataGridJson;
import com.core.common.pageModel.JsonMessage;
import com.core.life.service.IConsumeItemService;
import com.core.life.vo.ConsumeItemVo;

/**
 * 消费项Controller
 *
 */
@Controller
@RequestMapping("admin/consumeItem")
public class ConsumeItemController extends BaseController {

	@Resource
	private IConsumeItemService consumeItemService;
	
	@RequestMapping("/index")
	public String index() {
		return "admin/consumeItem/index";
	}
	
	//获取用户列表
	@RequestMapping(value = "/getList", method = RequestMethod.POST)
	@ResponseBody
	//public EasyuiDataGridJson getUserList(int page, int rows,String sort, String order, HttpServletRequest request) {
	public EasyuiDataGridJson getUserList(HttpServletRequest request) {
		EasyuiDataGridJson json = new EasyuiDataGridJson();
		Pager<ConsumeItemVo> pager = consumeItemService.getList(null);
		if(pager != null){
			json.setTotal(pager.getTotalCount());
			json.setRows(pager.getDatas());			
		}
		if(null == json.getRows()){
			json.setTotal(0l);
			json.setRows(new ArrayList<ConsumeItemVo>());	
		}
		return json;			
	}
	
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public JsonMessage create(ConsumeItemVo type) {
		JsonMessage jsonMsg = new JsonMessage();
		if (consumeItemService.existTypeName(type)) {
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("名称已存在！");
			jsonMsg.setObj("名称已存在！");
			return jsonMsg;
		} 
		try {
			consumeItemService.save(type);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("添加消费项目出错！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("添加消费项目成功！");
		return jsonMsg;
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public JsonMessage edit(ConsumeItemVo type) {
		JsonMessage jsonMsg = new JsonMessage();
		if (consumeItemService.existTypeName(type)) {
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("名称已存在！");
			jsonMsg.setObj("名称已存在！");
			return jsonMsg;
		}
		try {
			consumeItemService.update(type);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("修改消费项目出错！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("修改消费项目成功！");
		return jsonMsg;
	}
	
	@RequestMapping(value = "/delete")
	@ResponseBody
	public JsonMessage delete(String ids) {
		JsonMessage jsonMsg = new JsonMessage();
		try {
			consumeItemService.delete(ids);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("删除消费项目出错！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("删除消费项目成功！");
		return jsonMsg;
	}
	
	@RequestMapping(value = "/getConsumeItemComboList")
	@ResponseBody
	List<Combo> getConsumeItemComboList(Long id){
		return consumeItemService.getConsumeItemComboList(id);
	}
}
